package com.nlp.visualization.controller.jersey.controller;

import com.google.gson.Gson;
import com.nlp.visualization.common.CONSTANTS;
import com.nlp.visualization.controller.jersey.error.ErrorCode;
import com.nlp.visualization.core.discourse.IBayesService;
import com.nlp.visualization.core.format.FormatUtils;
import com.nlp.visualization.core.sentence.SentenceType;
import com.nlp.visualization.core.sentence.SentenceUtils;
import com.nlp.visualization.dao.weibo.IWeiboContentMod0;
import com.nlp.visualization.pojo.NLP.sen.SentenceEntity;
import com.nlp.visualization.pojo.common.AjaxReturn;
import com.nlp.visualization.pojo.weibo.TWeiboContentMod0;
import com.nlp.visualization.service.IDataDiscourseService;
import com.nlp.visualization.service.IDataWeiboService;
import com.nlp.visualization.service.weibo_mine.IWeiboContentMod0Service;
import com.nlp.visualization.service.weibo_mine.IWeiboFollowService;
import com.nlp.visualization.service.weibo_mine.IWeiboUserinfoService;
import com.nlp.visualization.utils.ConvertUtils;
import org.apache.commons.collections.map.HashedMap;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author LXM
 * @Title: 微博页面控制类
 * @Description: WeiboController
 * @date 2018/3/15下午4:15
 */
@Controller("WeiboController")
@Path("/weibo")
public class WeiboController {

    private Logger logger = LoggerFactory.getLogger(WeiboController.class);

    private Gson gson = new Gson();

    @Autowired
    IDataWeiboService dataWeiboService;

    @Autowired
    IBayesService bayesService;


    /**
     * 用户接口，爬取下用户数据
     *
     * @param json
     * @return
     */
    @GET
    @Path("/u/{id}")
    @Produces(MediaType.APPLICATION_JSON)
    public String getUserWeibo(@PathParam("id") final String uid) {

        AjaxReturn ajaxReturn = new AjaxReturn();
        try {
            dataWeiboService.doCrawlWeibo(uid);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return gson.toJson(new AjaxReturn(CONSTANTS.ERROR_SUCCESS, "请求成功"));
    }


    /**
     * 用户接口，爬取下用户数据
     *
     * @param json
     * @return
     */
    @GET
    @Path("/user/follow/{id}")
    @Produces(MediaType.APPLICATION_JSON)
    public String getUserFollow(@PathParam("id") final String uid) {

        AjaxReturn ajaxReturn = new AjaxReturn();
        try {
            dataWeiboService.doCrawFollow(uid);
        } catch (Exception e) {
            e.printStackTrace();
        }


        return gson.toJson(new AjaxReturn(CONSTANTS.ERROR_SUCCESS, "请求成功"));
    }


    @GET
    @Path("/user/{id},{count}")
    @Produces(MediaType.APPLICATION_JSON)
    public String getWeiboUserAllInfo(@PathParam("id") final String uid, @PathParam("count") final int count) {

        Map resultMap = new HashedMap();
        try {
            List weibo = dataWeiboService.getAllWeibo(uid);
            Map userInfo = dataWeiboService.getWeiboUserInfo(uid);
            List follow = dataWeiboService.getAllFollow(uid, count);
            resultMap.put("userInfo", userInfo);
            resultMap.put("weibo", weibo);
            resultMap.put("follow", follow);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return gson.toJson(new AjaxReturn(CONSTANTS.ERROR_SUCCESS, "请求成功", resultMap));
    }


    @GET
    @Path("/emotion/{id}")
    @Produces(MediaType.APPLICATION_JSON)
    public String getEmotionByID(@PathParam("id") final String uid) {

        List weibo = dataWeiboService.getAllWeibo(uid);
        List<String> list = new ArrayList<>();
        for (Object w : weibo) {
            Map map = (Map) w;
            list.add((String) map.get("text"));


        }

        Map map = bayesService.predict("8CB97B845FBDFA6EEE02E6E05941E110", list);
        return gson.toJson(new AjaxReturn(CONSTANTS.ERROR_SUCCESS, "请求成功", map));

    }

}
